﻿package com.game.view.component.notification
{
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.manager.GameSceneManager;
	import com.game.view.component.ComponentBase;
	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormatAlign;
	
	public class TipBox extends ComponentBase
	{
		public function TipBox(content:String = "", confirmFunc:Function = null, cancelFunc:Function = null, displayCancel:Boolean = true):void
		{
			super();
			if (content != "")
			{
				this.txt.text = content;
			}
			if (confirmFunc != null)
			{
				this.confirmFunc = confirmFunc;
				this.confirmBtn.addEventListener(MouseEvent.CLICK, this.confirmFunc);
			}
			if (cancelFunc != null)
			{
				this.cancelFunc = cancelFunc;
				this.cancelBtn.addEventListener(MouseEvent.CLICK, this.cancelFunc);
			}
			if (displayCancel == false)
			{
				cancelBtn.visible = false;
				confirmBtn.x = (confirmBtn.x + cancelBtn.x) * 0.5;
			}
			this.confirmBtn.addEventListener(MouseEvent.CLICK, destroy);
			this.cancelBtn.addEventListener(MouseEvent.CLICK, destroy);
			this.alpha = 0;
			TweenMax.to(this, 0.2, {"alpha": 1});
		}
		
		public var txt:TextField;
		public var confirmBtn:SimpleButton;
		public var cancelBtn:SimpleButton;
		private var confirmFunc:Function = null;
		private var cancelFunc:Function = null;
		
		override protected function beforeAdded():void
		{
			var view:MovieClip = ResUtil.getComponentView("TipBox") as MovieClip;
			//noinspection SpellCheckingInspection
			confirmBtn = view.getChildByName("okbtn") as SimpleButton;
			cancelBtn = view.getChildByName("nobtn") as SimpleButton;
			addChild(view);
			txt = TextUtil.creatTextField("",TextUtil.UNIVERSAL_FONT,14,TextFormatAlign.CENTER,0xFFFFFF,true,false);
			with (txt)
			{
				x = 357;
				y = 260;
				width = 190;
				height = 60;
			}
			addChild(txt);
		}
		
		public function add():void
		{
			GameSceneManager.getIns().addScene(this,"top");
		}
		
		public function setText(param1:String):void
		{
			this.txt.text = param1;
		}
		
		public function appendText(param1:String):void
		{
			this.txt.appendText(param1);
		}
		
		public function setConfirmFunc(param1:Function):void
		{
			if (this.confirmFunc != null)
			{
				return;
			}
			this.confirmFunc = param1;
			this.confirmBtn.addEventListener(MouseEvent.CLICK, this.confirmFunc);
		}
		
		public function setCancelFunc(param1:Function):void
		{
			if (this.cancelFunc != null)
			{
				return;
			}
			this.cancelFunc = param1;
			this.cancelBtn.addEventListener(MouseEvent.CLICK, this.cancelFunc);
		}
		
		public function hideBtn():void
		{
			if (!this.cancelBtn.visible)
			{
				return;
			}
			cancelBtn.visible = false;
			confirmBtn.x = (confirmBtn.x + cancelBtn.x) * 0.5;
		}
		
		private function destroy(param1:MouseEvent):void
		{
			this.confirmBtn.removeEventListener(MouseEvent.CLICK, this.destroy);
			this.cancelBtn.removeEventListener(MouseEvent.CLICK, this.destroy);
			if (this.confirmFunc != null)
			{
				this.confirmBtn.removeEventListener(MouseEvent.CLICK, this.confirmFunc);
			}
			if (this.cancelFunc != null)
			{
				this.cancelBtn.removeEventListener(MouseEvent.CLICK, this.cancelFunc);
			}
			TweenMax.to(this, 0.2, {"alpha": 0});
			TweenMax.delayedCall(0.3, function (box:TipBox):*
			{
				box.removeFromParent();
			}, [this]);
		}
	}
}